﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Silvermoon.UI;

namespace Silvermoon.Navigation
{
    internal class PageNode
    {
        /// <summary>
        /// Page instance.
        /// </summary>
        public Page Page { get; set; }

        public Type PageType { get; private set; }

        /// <summary>
        /// Gets or sets the parent PageNode.
        /// </summary>
        public PageNode Parent { get; set; }

        /// <summary>
        /// View state of the disposed page.
        /// </summary>
        internal protected object ViewState;

        public PageNode(Page page)
        {
            if (page == null) throw new ArgumentNullException("page");
            PageType = page.GetType();
            this.Page = page;
        }


        internal Page ActivatePage(NavigationService navigator)
        {
            try
            {
                Page page = (Page)Activator.CreateInstance(PageType);
                page.NavigationService = navigator;
                return page;
            }
            catch (Exception ex)
            {
                throw new InvalidCastException("Could not create page instance of type" + PageType.Name + ".", ex);
            }

        }
    }
}
